package com.itheima.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.result.Result;
import com.itheima.configMessage.ConfigValue;
import com.itheima.dto.SysConfigDTO;
import com.itheima.configMessage.SysConfigRules;
import com.itheima.mapper.SysConfigMapper;
import com.itheima.pojo.SysConfig;
import com.itheima.service.ISysConfigService;
import com.itheima.vo.SysConfigGroupVO;
import com.itheima.vo.SysConfigTabVO;
import com.itheima.vo.SysConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ISysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 获取所有配置文件
     *
     * @return
     */
    @Override
    public Result ListSysConfig() {
        //1.获取配置表所有信息
        List<SysConfig> allData = sysConfigService.list();
        //设置复选框属性
        ConfigValue configValue1 = new ConfigValue("是","1");
        ConfigValue configValue2 = new ConfigValue("否","2");

        //使用DTO封装关键信息
        List<SysConfigDTO> newAllData = new ArrayList<>();
        allData.forEach(d -> {
            //给configvalue赋值
            SysConfigDTO dto = new SysConfigDTO();
            BeanUtils.copyProperties(d,dto);
            newAllData.add(dto);
        });

        //2.tab层获取配置标签信息
        //list集合封装tab配置设置数据
        List<SysConfigTabVO> list = new ArrayList<>();
        //获取tab设置名字
        QueryWrapper<SysConfig> lqw = new QueryWrapper<>();
        lqw.select("distinct tab_value");
        List<SysConfig> tabNames = sysConfigService.list(lqw);
        //对设置名称命名
        tabNames.forEach(tabName -> {
            List<SysConfigGroupVO> children = new ArrayList<>();
            //tabVO封装单个设置数据
            SysConfigTabVO tabVO = new SysConfigTabVO();
            String tabValue = tabName.getTabValue();
            //设置该tab值的设置名
            switch (tabValue) {
                case "base":
                    tabVO.setGroupValue("基础设置");
                    tabVO.setTabValue(tabValue);
                    break;
                case "custom":
                    tabVO.setGroupValue("其他设置");
                    tabVO.setTabValue(tabValue);
                    break;
                case "system":
                    tabVO.setGroupValue("系统设置");
                    tabVO.setTabValue(tabValue);
                    break;
            }
        //group层
            //获取当前tab下的group名字
            QueryWrapper<SysConfig> newLqw = new QueryWrapper<>();
            newLqw.select("group_value").eq("tab_value", tabValue).groupBy("group_value");
            List<SysConfig> distinctGroupNames = sysConfigService.list(newLqw);
            //对groups遍历，从总表中获取当前group的所有信息
            distinctGroupNames.forEach(distinctGroupName -> {

                SysConfigGroupVO groupVO = new SysConfigGroupVO();
                //2.2获取当前设置下对应的组件名称表，嵌套查询
                List<SysConfig> groupData = sysConfigMapper.listByTabAndGroup(tabValue, distinctGroupName.getGroupValue());
                //创建新的组件名称表接收带复选框的vo对象
                List NewGroupData = new ArrayList<>();
                groupData.forEach(g->{
                    //创建复选框对象
                    List configValueList = new ArrayList();
                    configValueList.add(configValue1);
                    configValueList.add(configValue2);
                    //创建vo对象拷贝group对象并给给其复选框对象赋值
                    SysConfigVO sysConfigVO = new SysConfigVO();
                    BeanUtils.copyProperties(g,sysConfigVO);
                    sysConfigVO.setConfig_value(configValueList);
                    //新建的group数据集合添加数据
                    NewGroupData.add(sysConfigVO);
                });

                groupVO.setGroupValue(distinctGroupName.getGroupValue());
                //获取当前group分组配置全部信息并封装到groupVO
                groupVO.setData(NewGroupData);
                //将全部设置数据封装到children
                children.add(groupVO);
            });
            //将children封装到tabVO
            tabVO.setChildren(children);
            //list集合封装设置数据
            list.add(tabVO);
        });

        //3.获取configForm表单
        Map configFormMap = new HashMap();
        //新建sysConfigRules封装siteName
        SysConfigRules sysConfigRules = new SysConfigRules();
        //遍历全部配置数据
        allData.forEach(d -> {
            configFormMap.put(d.getAttrKey(), d.getAttrValue());
            if (d.getAttrKey().equals("site_name")) {
                sysConfigRules.setSiteName(d.getValidator());
            }
        });
        Map map = new HashMap();
        map.put("list",list);
        map.put("allData",newAllData);
        map.put("configForm",configFormMap);
        map.put("configRules",sysConfigRules);

        return Result.success(map);

    }
    /**
     * 修改配置文件
     * @param dto
     * @return
     */
    @Override
    public void updateSysConfig(List<SysConfigDTO> dto) {
        //获取配置表的全部信息
        List<SysConfig> configList = sysConfigService.list();
        //遍历全部信息
        configList.forEach(config->{
            //遍历修改配置的表
            dto.forEach(sysConfigDTO -> {
                //通过ID修改总表
                if(config.getId()==sysConfigDTO.getId()){
                    config.setAttrKey(sysConfigDTO.getAttrKey());
                    config.setAttrValue(sysConfigDTO.getAttrValue());
                    //更新总表数据
                    sysConfigService.updateById(config);
                }
            });
        });
    }
}
